//
// FileTool
//
// Copyright (C) jextra.net.
//
//  This file is part of the Crack build system.
//
//  The Crack build system is free software; you can redistribute it 
//  and/or modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The Crack build system is distributed in the hope that it will be 
//  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the Crack build system; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.crack.tool;

import java.io.*;

/**
 * <p>
 * Useful methods to call on {@link File} files and/or directories.
 * </p>
 */
public class FileTool
{
    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    public static boolean makeDir( File dir )
    {
        return dir.mkdirs();
    }

    /**
     * Remove everything from the given directory, but keep the directory.
     */
    public static boolean cleanDir( File dir )
    {
        return deleteDir( dir, false );
    }

    /**
     * Delete the given directory and everything in it.
     */
    public static boolean deleteDir( File dir )
    {
        return deleteDir( dir, true );
    }

    public static String getRelativePath( File rootDir, File file )
        throws IOException
    {
        String rootString = rootDir.getCanonicalPath().replace( '\\', '/' );
        while ( rootString.endsWith( "/" ) )
        {
            rootString = rootString.substring( 0, rootString.length() - 1 );
        }
        String path = file.getCanonicalPath().replace( '\\', '/' );
        assert path.startsWith( rootString ) : String.format( "%s is not part of the path %s", path,
            rootString );
        if ( path.length() == rootString.length() )
        {
            return "";
        }

        path = path.substring( rootString.length() + 1 );
        return path;
    }

    // ----------
    // private
    // ----------

    private static boolean deleteDir( File dir, boolean deleteSelf )
    {
        assert dir != null : "Directory can not be null";

        // Already does not exist.
        if ( !dir.exists() )
        {
            return true;
        }

        assert dir.isDirectory() : "'" + dir + "' must be a directory.";

        for ( File file : dir.listFiles() )
        {
            boolean success = true;
            if ( file.isDirectory() )
            {
                success = deleteDir( file, true );
            }
            else
            {
                success = file.delete();
            }

            if ( !success )
            {
                return false;
            }
        }

        // The directory is now empty so delete it
        if ( deleteSelf )
        {
            return dir.delete();
        }

        return true;
    }
}

